package com.oocl.pmsystem.model.impl;

import com.oocl.pmsystem.bean.Person;
import com.oocl.pmsystem.bean.comparator.SuperComparator;
import com.oocl.pmsystem.bean.condition.Condition;
import com.oocl.pmsystem.bean.condition.UpdateCondition;
import com.oocl.pmsystem.db.PersonListFactory;
import com.oocl.pmsystem.db.SuperLinkedList;
import com.oocl.pmsystem.model.DBConnection;

public class PersonDBConnection implements DBConnection<Person>{

	////持有 自定义双向链表的数据结构引用，用于存储和管理数据
	private SuperLinkedList<Person> mList;
	
	public PersonDBConnection() {
		mList = PersonListFactory.getInstance().createData();
	}
	
	
	@Override
	public boolean connect() {
		return false;
	}

	@Override
	public boolean add(Person t) {
		return this.mList.add(t);
	}

	@Override
	public Person[] query(Condition<Person> condition) {
		Object[] list =this.mList.query(condition);
		if(list==null ||list.length==0) return null;
		Person[] persons = new Person[list.length];
		int i =0;
		for(Object item:list) {
			persons[i++] = (Person)item;
//			System.out.println("PersonDBConnection.Persons = "+ persons[i-1]);
		}
		return persons;
	}

	@Override
	public boolean delete(Condition<Person> condition) {
		return this.mList.delete(condition);
	}

	@Override
	public boolean update(UpdateCondition<Person> condition) {
		return this.mList.update(condition);
	}

	@Override
	public void clear() {
		this.mList.clear();
	}

	@Override
	public Person[] sort(SuperComparator<Person> superComparator) {
		Object[] list = this.mList.sort(superComparator);
		if(list==null ||list.length==0) return null;
		Person[] persons = new Person[list.length];
		int i =0;
		for(Object item:list) {
			persons[i++] = (Person)item;
//			System.out.println("PersonDBConnection.Persons = "+ persons[i-1]);
		}
		return persons;
	}

}
